package com.itheima.book.manager.dao;

import com.itheima.book.manager.domain.Book;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class BookFileDao implements BaseBookDao {

    private static final String BOOK_INFO_PATH = "itheima-book-manager\\bookInfo.txt";
    private static final String DATE_PATTER = "yyyy-MM-dd";

    @Override
    public boolean addBook(Book book) {

        BufferedWriter bufferedWriter = null;
        try{
            bufferedWriter = new BufferedWriter(new FileWriter(BOOK_INFO_PATH, true));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTER);
            String publishDateStr = simpleDateFormat.format(book.getPublishDate());
            String bookStr = book.getBookID() + "," + book.getBookName() + "," + publishDateStr + "," + book.getPrice();
            bufferedWriter.write(bookStr);
            bufferedWriter.newLine();
            bufferedWriter.flush();
            return true;
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                if (bufferedWriter != null){
                    bufferedWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public Book[] getAllBooks() {
        ArrayList<Book> arrayList = new ArrayList<Book>();
//        Book[] books = new Book[arrayList.size()];
        BufferedReader bufferedReader = null;
        try{
            File file = new File(BOOK_INFO_PATH);
            if (!file.exists()){
                file.createNewFile();
            }
            bufferedReader = new BufferedReader(new FileReader(BOOK_INFO_PATH));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {

                String[] split = line.split(",");
                String bookId = split[0];
                String bookName = split[1];
                String publishDateStr = split[2];
                String price = split[3];
                Book book = new Book();
                book.setBookID(bookId);
                book.setBookName(bookName);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTER);
                Date publishDateDate = simpleDateFormat.parse(publishDateStr);
                book.setPublishDate(publishDateDate);
                double doublePrice = Double.parseDouble(price);
                book.setPrice(doublePrice);
                arrayList.add(book);
                Book[] books = new Book[arrayList.size()];
                for (int i = 0; i < arrayList.size(); i++) {
                    books[i] = arrayList.get(i);
                }
                return books;
            }
//            return books;
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null){
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new Book[0];
    }

    @Override
    public void deleteByBookId(String bookId) {
        Book[] allBooks = getAllBooks();
        int index = -1;
        for (int i = 0; i < allBooks.length; i++) {
            if (allBooks[i] != null){
                if (allBooks[i].getBookID().equals(bookId)){
                    index = i;
                    break;
                }
            }
        }
        if (index != -1){
            allBooks[index] = null;
        }
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(BOOK_INFO_PATH));
            for (Book book : allBooks){
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTER);
                String publishStr = simpleDateFormat.format(book.getPublishDate());
                String bookStr = book.getBookID() + "," + book.getBookName() + "," + publishStr + "," + book.getPrice();
                bufferedWriter.write(bookStr);
                bufferedWriter.newLine();
                bufferedWriter.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void updateBookByBookId(Book book) {
        Book[] allBooks = getAllBooks();
        int index = -1;
        for (int i = 0; i < allBooks.length; i++) {
            if (allBooks[i].getBookID().equals(book.getBookID())){
                index = i;
                break;
            }
        }
        if (index != -1) {
            allBooks[index] = book;
        }
        BufferedWriter bufferedWriter = null;
        try{
            for (int i = 0; i < allBooks.length; i++) {
                Book book1 = allBooks[i];
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTER);
                String publishDateStr = simpleDateFormat.format(book1.getPublishDate());
                String bookStr = book1.getBookID() + "," + book1.getBookName() + "," + book1.getPublishDate() + "," + book1.getPrice();
                bufferedWriter = new BufferedWriter(new FileWriter(BOOK_INFO_PATH));
                bufferedWriter.write(bookStr);
                bufferedWriter.newLine();
                bufferedWriter.flush();
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            if (bufferedWriter != null){
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
